package com.guli.mall.wms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.guli.common.to.SkuHasStockTo;
import com.guli.common.to.StockWareTo;
import com.guli.mall.wms.entity.vo.WareSkuNameVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;

import com.guli.mall.wms.entity.WareSkuEntity;
import com.guli.mall.wms.service.WareSkuService;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;


/**
 * 商品库存
 *
 * @author 罗俊华
 * @email 1007052116@qq.com
 * @date 2020-09-29 21:37:28
 */
@RestController
@RequestMapping("wms/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;


    @GetMapping("/list/{currentPage}/{size}")
    @ApiOperation("根据当前页和每页显示的记录数来获取wareSku分页列表")
    public R list(@PathVariable("currentPage") Integer currentPage, @PathVariable("size") Integer size,
                  @RequestParam Map<String,String> map) {
        PageUtils<WareSkuNameVo> page = wareSkuService.queryPage(currentPage, size,map);

        return R.ok().put("page", page);
    }


    @GetMapping("/info/{id}")
    @ApiOperation("根据id获取WareSku")
    public R info(@PathVariable("id") Long id) {
        WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    @PostMapping("/save")
    @ApiOperation("插入WareSku")
    public R save(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.save(wareSku);

        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("根据id更新WareSku")
    public R update(@RequestBody WareSkuEntity wareSku) {
        wareSkuService.updateById(wareSku);

        return R.ok();
    }

    @PutMapping("/deleteBatch")
    @ApiOperation("根据id批量删除WareSku")
    public R delete(@RequestBody Long[] ids) {
        wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/getWareSkuBySkuId/{skuId}")
    public WareSkuEntity getWareSkuBySkuId(@PathVariable("skuId")Long skuId){

        return wareSkuService.getWareSkuBySkuId(skuId);
    }

    @PostMapping("/judgeStockExist")
    @ApiOperation("通过skuId判断该sku是否有库存")
    public R judgeStockExist(@RequestBody List<Long> skuId){
        List<SkuHasStockTo> skuHasStockTos = wareSkuService.judgeStockExist(skuId);
        return R.ok().setData(skuHasStockTos);
    }

    @ApiOperation("根据skuId，来批量查询库存，skuId对应的库存存放在Map的value位置")
    @PostMapping("/getStockBySkuIdArray")
    public Map<Long,Integer> getStockBySkuIdArray(@RequestBody List<Long> skuIds){

        return wareSkuService.getStockBySkuIdArray(skuIds);
    }

    @ApiOperation("用户下单时，锁定库存")
    @PostMapping("/lockWareStock")
    public R lockWareStock(@RequestBody StockWareTo stockWareTo){

        wareSkuService.lockWareStock(stockWareTo);

        return R.ok();
    }

}
